package com.iwith.submail_one_login_plugin

import android.content.Context
import com.cmic.gen.sdk.view.GenAuthThemeConfig
import com.submail.onelogin.sdk.client.SubSDK

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** SubmailOneLoginPlugin */
class SubmailOneLoginPlugin : FlutterPlugin, MethodCallHandler {
    private lateinit var channel: MethodChannel
    private lateinit var context: Context


    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        context = flutterPluginBinding.applicationContext
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.xdr.sub_oneLogin")
        channel.setMethodCallHandler(this)
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        when (call.method) {
            "init" -> subSDKInit(call, result)
            "getLoginAccessCode" -> getLoginAccessCode(result)
            "getLoginToken" -> getLoginToken(result)
            "close" -> SubSDK.quitActivity(context)
            "getNetworkType" -> result.success(SubSDK.getNetworkType(context))
            "getOperatorType" -> result.success(SubSDK.getOperatortype(context))
            "delScrip" -> SubSDK.delScrip(context)
            "getPhoneNumber" -> getPhoneNumber(result)
            else -> {
                result.notImplemented()
            }
        }
    }


    private fun subSDKInit(call: MethodCall, result: Result) {
        val appId = call.argument<String>("androidAppId")
        val appKey = call.argument<String>("androidAppKey")
        val debug = call.argument<Boolean>("debug") ?: false
        val timeout = call.argument<Int>("timeout") ?: 60
        try {
            SubSDK.isShowLog(debug)
            SubSDK.setTimeout(timeout)
            SubSDK.init(
                context, appId, appKey
            ) { success, res ->
                result.success(
                    mapOf(
                        "success" to success,
                        "result" to res
                    )
                )
            }
        } catch (e: Exception) {
            result.error("-1", e.message, e)
        }
    }

    private fun getLoginAccessCode(result: Result) {
        try {
            SubSDK.getLoginAccessCode(context) { success, res ->
                result.success(
                    mapOf(
                        "success" to success,
                        "result" to res
                    )
                )
            }
        } catch (e: Exception) {
            result.error("-1", e.message, e)
        }
    }

    private fun getLoginToken(result: Result) {
        try {
            SubSDK.getLoginToken(
                context,
                GenAuthThemeConfig.Builder().build()
            ) { success, res ->
                result.success(
                    mapOf(
                        "success" to success,
                        "result" to res
                    )
                )
            }
        } catch (e: Exception) {
            result.error("-1", e.message, e)
        }
    }

    private fun getPhoneNumber(result: Result) {
        try {
            SubSDK.getPhoneNumber(context) { success, res ->
                result.success(
                    mapOf(
                        "success" to success,
                        "result" to res
                    )
                )
            }
        } catch (e: Exception) {
            result.error("-1", e.message, e)
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }

}
